import store from '@/store/index'
const baseUrl = 'https://api-hmugo-web.itheima.net/api/public/v1/'

const request = ({
	url,
	method = 'GET',
	header = {},
	data = {},
	tip = '拼命加载中',
	isTip = true
}) => {
	return new Promise((resolve, reject) => {
		if (isTip) {
			uni.showLoading({
				title: tip,
				mask: true
			})
		}
		// const token = store.getters['user/getToken']
		const token = store.state.user.token
		if (token) {
			header.Authorization = token
		}

		uni.request({
			url: `${baseUrl}${url}`,
			data,
			header,
			method,
			success: res => {
				resolve(res.data)
			},
			fail: error => {
				reject(error)
			},
			complete: () => {
				if (isTip) {
					uni.hideLoading()
				}
			}
		})
	})

}

uni.$request = request

export {
	request
}